
/* socket related definitions */
#ifndef _WIN32
#define SOCKET int
#endif

#ifndef SOCKET_ERROR
#define SOCKET_ERROR -1
#endif

#ifdef _WIN32
#define socket_errno() WSAGetLastError()
#else /* !_WIN32 */
#define closesocket close
#define socket_errno() (errno)
#endif /* !_WIN32 */


#define BUF_SIZE 1024

/* report flag to hide secure information */
extern int f_report ;
extern int f_debug;
extern int seconds_timeout;

#define PUT_BYTE(ptr,data) (*(unsigned char*)ptr = data)

void debug( const char *fmt, ... );
void fatal( const char *fmt, ... );
void report_bytes( char *prefix, char *buf, int len );

int atomic_out( SOCKET s, void *buf, int size );
int atomic_in( SOCKET s, void *buf, int size );

int connect_nonb(int sockfd, const struct sockaddr *saptr, socklen_t salen, int *timeout);

unsigned long shuffle_bytes(int client, int server) ;
unsigned long shuffle_bytes_encode(int client, int server) ;

#define MAX_INTERFACE 20
#define   LISTENQ   1024

/*****************************************************************************
*** Get the IP address of interface ifName in address family addrFamily
*** If succeed, return 0, and the result will be stored in a string  'ipAddress'
*** ipAddress must NOT be NULL, and the length of ipAddress is pointed with
*** 'len_ip';
*** If failed , return -1;
********************************************************************************/
int get_ip_if(char * ifName, int addrFamily, char *ipAddress, int len_ip);

/** ifName is the returned ifname, ipAddress is the ip string  **/
int get_first_if(char * ifName, int addrFamily, char *ipAddress, int len_ip);


//Get the remote ip and port of  socket_fd,
// return 0 if success; or -1 ;
// *ipAddress and *port are both in host byte order
int get_remote_socket_info(int socket_fd, struct in_addr *ipAddress,int * port);

#define ATOMIC_IN(sock, buf,length,msg) \
        { \
                if(length!=atomic_in(sock, buf,length)) {\
                    debug("Error atomic_in:%s",msg);\
                    return -1;\
                }\
        }
#define ATOMIC_OUT(sock, buf,length,msg) \
        { \
                if(length!=atomic_out(sock, buf,length)) {\
                    debug("Error atomic_out:%s",msg);\
                    return -1;\
                }\
        }



